#include "folderslistwindow.h"
#include "ui_folderslistwindow.h"

foldersListWindow::foldersListWindow(QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::foldersListWindow)
{
    m_ui->setupUi(this);
    m_ui->removeButton->setEnabled( false );
}

foldersListWindow::~foldersListWindow()
{
    delete m_ui;
}

void foldersListWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void foldersListWindow::run_form()
{
    QRect frect = frameGeometry();
    frect.moveCenter(QDesktopWidget().availableGeometry().center());
    move(frect.topLeft());
}

void foldersListWindow::on_addFtpButton_clicked()
{
    inputFolderDialog inputFolder( this );
    ftpInfo * myInfo = inputFolder.getFtpInfo();
    if ( myInfo )
    {
        QString ftpPath = myInfo->hostname;

        if ( isSuchPath(ftpPath) )
        {
            QMessageBox::critical( this, "Index List", "There is already such path in the list" );
            return;
        }

        addPathToList( myInfo );
    }
}

QString foldersListWindow::getLastSourceFolder() const
{
    QString retStr;

    QSettings settings("Folder index", "Settings");
    settings.beginGroup("Folders");
        retStr = settings.value("last_source_folder", "" ).toString();
    settings.endGroup();

    return retStr;
}

void foldersListWindow::setLastSourceFolder( const QString &path )
{
    QSettings settings("Folder index", "Settings");
    settings.beginGroup("Folders");
    settings.setValue("last_source_folder", path );
    settings.endGroup();
}

bool foldersListWindow::isSuchPath( const QString &path )
{
    if ( m_ui->treeWidget->findItems( path, Qt::MatchExactly, 0).size() > 0 )
        return true;
    return false;
}

void foldersListWindow::on_addFolderButton_clicked()
{
    QString dirPath = QFileDialog::getExistingDirectory ( this,
                                                          "Choose folder",
                                                          getLastSourceFolder() );
    if ( isSuchPath(dirPath) )
    {
        QMessageBox::critical( this, "Index List", "There is already such path in the list" );
        return;
    }

    if ( !dirPath.isEmpty() )
    {
        setLastSourceFolder( dirPath );
        addPathToList( dirPath, "Folder" );
    }
}

void foldersListWindow::addPathToList( const QString& path, const QString& type )
{
    if ( !path.isEmpty() )
    {
        QTreeWidgetItem * item = new QTreeWidgetItem( m_ui->treeWidget, 0 );
        item->setText( 0, path );
        item->setText( 1, type );

        m_ui->treeWidget->setCurrentItem( item );

        // count items
        QTreeWidgetItemIterator iter1( m_ui->treeWidget );
        int ftp = 0, fol = 0;
        while (*iter1)
        {
            if ( (*iter1)->text(1) == "Folder" )
                fol++;
            else
                ftp++;
            ++iter1;
        }

        statusBar()->showMessage ( QString("Total folders - %1   Total ftp servers - %2")
                                   .arg(fol).arg(ftp) );
        for ( int i = 0; i < m_ui->treeWidget->columnCount(); i++ )
            m_ui->treeWidget->resizeColumnToContents(i);
    }
}

void foldersListWindow::addPathToList( ftpInfo* cFtp )
{
    if ( cFtp )
    {
        QTreeWidgetItem * item = new QTreeWidgetItem( m_ui->treeWidget, 0 );
        item->setText( 0, cFtp->hostname );
        item->setText( 1, "Ftp" );
        item->setText( 2, cFtp->username );
        item->setText( 3, cFtp->password );
        item->setText( 4, cFtp->remoteDir );

        m_ui->treeWidget->setCurrentItem( item );

        // count items
        QTreeWidgetItemIterator iter1( m_ui->treeWidget );
        int ftp = 0, fol = 0;
        while (*iter1)
        {
            if ( (*iter1)->text(1) == "Folder" )
                fol++;
            else
                ftp++;
            ++iter1;
        }

        statusBar()->showMessage ( QString("Total folders - %1   Total ftp servers - %2")
                                   .arg(fol).arg(ftp) );
        for ( int i = 0; i < m_ui->treeWidget->columnCount(); i++ )
            m_ui->treeWidget->resizeColumnToContents(i);
    }
}

void foldersListWindow::on_proceedButton_clicked()
{
    if ( m_ui->treeWidget->topLevelItemCount() > 0 )
    {
        QStringList folders;
        QList<ftpInfo*> ftpInfoList;
        QTreeWidgetItemIterator iter1( m_ui->treeWidget );
        while (*iter1)
        {
            if ( (*iter1)->text(1) == "Folder" )
                folders.append( (*iter1)->text( 0 ) );
            else
            {
                ftpInfo * fInfo = new ftpInfo();
                fInfo->hostname = (*iter1)->text(0);
                fInfo->username = (*iter1)->text(2);
                fInfo->password = (*iter1)->text(3);
                fInfo->remoteDir = (*iter1)->text(4);
                ftpInfoList.append(fInfo);
            }
            ++iter1;
        }
        MainWindow * listWind1 = new MainWindow();
        listWind1->setFullRootPathShow( true );
        listWind1->show();
        listWind1->run_form( folders, ftpInfoList );
    }
    else
    {
        QMessageBox::critical( this, "Select folders", "You must specify at least one folder or ftp");
        return;
    }
    close();
}

void foldersListWindow::on_cancelButton_clicked()
{
    close();
}

void foldersListWindow::on_removeButton_clicked()
{
    if ( m_ui->treeWidget->currentItem() )
    {
        m_ui->treeWidget->takeTopLevelItem(
                m_ui->treeWidget->indexOfTopLevelItem( m_ui->treeWidget->currentItem() ) );

        // count items
        QTreeWidgetItemIterator iter1( m_ui->treeWidget );
        int ftp = 0, fol = 0;
        while (*iter1)
        {
            if ( (*iter1)->text(1) == "Folder" )
                fol++;
            else
                ftp++;
            ++iter1;
        }

        statusBar()->showMessage ( QString("Total folders - %1   Total ftp servers - %2")
                                   .arg(fol).arg(ftp) );
    }
}

void foldersListWindow::on_treeWidget_itemChanged(QTreeWidgetItem* item, int )
{
    if ( item )
        m_ui->removeButton->setEnabled( true );
    else
        m_ui->removeButton->setEnabled( false );
}
